import pytest
import requests

from api.Recharge_api import RechargeApi
from api.reg_login_api import RegLoginApi
from common.assert_tools import common_assert
from common.read_html_tools import html_util
from common.read_json_data import read_json


class TestRecharge(object):

    def setup(self):
        # 创建session对象
        self.ses = requests.session()
        # 实例化登录模块封装的类
        self.login = RegLoginApi(self.ses)
        # 实例化充值模块封装的类
        self.recharge = RechargeApi(self.ses)

    def teardown(self):
        # 关闭session对象
        self.ses.close()

    # 获取充值验证码测试用例
    @pytest.mark.parametrize("r, exp_status_code", read_json("recharge_data", "recharge_verify_code"))
    def test01_get_recharge_verify_code(self, r, exp_status_code):
        # 获取充值图片验证码
        resp = self.recharge.get_recharge_verify_code(r)
        # 断言
        assert exp_status_code == resp.status_code

    # 充值测试用例
    @pytest.mark.parametrize("amount, img_code, exp_status_code, exp_status, exp_description", read_json("recharge_data", "recharge"))
    def test02_recharge(self, amount, img_code, exp_status_code, exp_status, exp_description):
        # 登录
        self.login.user_login("13155552220", "qq1234")
        # 获取充值图片验证码
        self.recharge.get_recharge_verify_code(1234567)
        resp = self.recharge.recharge(amount, img_code)
        # 断言
        common_assert(resp, exp_status_code, exp_status, exp_description)

     # 第三方充值测试用例
    def test03_third_recharge(self):
        # 登录
        self.login.user_login("13155552220", "qq1234")
        # 获取充值图片验证码
        self.recharge.get_recharge_verify_code(1234567)
        # 充值
        resp_recharge = self.recharge.recharge(100000, 8888)
        result = html_util(resp_recharge)
        # 获取第三方开户接口的url
        url = result[0]
        # 获取第三方开户接口的请求体数据
        form_data = result[1]
        # 获取第三方接口返回的响应结果
        resp = self.recharge.third_recharge(url, form_data)
        # 断言
        assert 200 == resp.status_code
        assert "OK" in resp.text
